package com.wanli.agent.homepage;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.AgentShowBean;
import com.wanli.agent.bean.BaseResponseBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

public class AgentDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.tv_setting2)
    TextView tvSetting2;
    @BindView(R.id.ll_agent_set)
    LinearLayout llAgentSet;
    @BindView(R.id.switchview_withdraw)
    SwitchView switchviewWithdraw;
    @BindView(R.id.switchview_login)
    SwitchView switchviewLogin;
    @BindView(R.id.switchview_income)
    SwitchView switchviewIncome;
    @BindView(R.id.switchview_invite)
    SwitchView switchviewInvite;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_income)
    TextView tvIncome;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.tv_look_parents)
    TextView tvLookParents;
    @BindView(R.id.tv_look_childs)
    TextView tvLookChilds;
    @BindView(R.id.tv_channel_type)
    TextView tv_channel_type;
    private String name;
    private String mobile;
    private String agent_id;
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private BottomSheetDialog selectPicDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_details);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    public void initView() {
        name = getIntent().getStringExtra("name");
        mobile = getIntent().getStringExtra("mobile");
        agent_id = getIntent().getStringExtra("agent_id");

        tvName.setText(name + "（" + mobile + "）");

        switchviewWithdraw.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                showPictureSelectorDialog("Withdraw", "1");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                showPictureSelectorDialog("Withdraw", "0");
            }
        });
        switchviewLogin.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                showPictureSelectorDialog("Login", "1");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                showPictureSelectorDialog("Login", "0");
            }
        });
        switchviewIncome.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                showPictureSelectorDialog("Income", "1");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                showPictureSelectorDialog("Income", "0");
            }
        });
        switchviewInvite.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                showPictureSelectorDialog("Invite", "1");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                showPictureSelectorDialog("Invite", "0");
            }
        });


        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            if (userInfoBean.getData().getId() == 1) {
                llAgentSet.setVisibility(View.VISIBLE);
            } else {
                llAgentSet.setVisibility(View.GONE);
            }
        }
    }


    public void initData() {
        homePageModel.requestAgentShow(agent_id, new DataCallBack<AgentShowBean>() {
            @Override
            public void onSuccessful(AgentShowBean bean) {
                if (bean.getData().getWithdraw_limit() == 0) {
                    switchviewWithdraw.setOpened(false);
                    tvWithdraw.setText("服务商提现：未限制");
                    tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                } else if (bean.getData().getWithdraw_limit() == 1) {
                    switchviewWithdraw.setOpened(true);
                    tvWithdraw.setText("服务商提现：已限制");
                    tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                }

                if (bean.getData().getLogin_limit() == 0) {
                    switchviewLogin.setOpened(false);
                    tvLogin.setText("服务商登录：未限制");
                    tvLogin.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                } else if (bean.getData().getLogin_limit() == 1) {
                    switchviewLogin.setOpened(true);
                    tvLogin.setText("服务商登录：已限制");
                    tvLogin.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                }
                if (bean.getData().getIncome_limit() == 0) {
                    tvIncome.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                    switchviewIncome.setOpened(false);
                    tvIncome.setText("服务商进件：未限制");
                } else if (bean.getData().getIncome_limit() == 1) {
                    switchviewIncome.setOpened(true);
                    tvIncome.setText("服务商进件：已限制");
                    tvIncome.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                }
                if (bean.getData().getInvite_limit() == 0) {
                    tvInvite.setText("服务商邀请：未限制");
                    tvInvite.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                    switchviewInvite.setOpened(false);
                } else if (bean.getData().getInvite_limit() == 1) {
                    switchviewInvite.setOpened(true);
                    tvInvite.setText("服务商邀请：已限制");
                    tvInvite.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                }

                int channel_type = bean.getData().getChannel_type();

                if (channel_type==2){
                    tv_channel_type.setVisibility(View.VISIBLE);
                }else {
                    tv_channel_type.setVisibility(View.GONE);
                }


            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 提现
     */
    public void requestChangeAgentWithdrawLimit(String limit, String agent_line) {
        homePageModel.requestChangeAgentWithdrawLimit(agent_id, limit, agent_line, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("设置成功");
                if ("1".equals(limit)) {
                    switchviewWithdraw.setOpened(true);
                    tvWithdraw.setText("服务商提现：已限制");
                    tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                } else {
                    switchviewWithdraw.setOpened(false);
                    tvWithdraw.setText("服务商提现：未限制");
                    tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 登录
     */
    public void requestChangeAgentLoginLimit(String limit, String agent_line) {
        homePageModel.requestChangeAgentLoginLimit(agent_id, limit, agent_line, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("设置成功");
                if ("1".equals(limit)) {
                    switchviewLogin.setOpened(true);
                    tvLogin.setText("服务商登录：已限制");
                    tvLogin.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                } else {
                    switchviewLogin.setOpened(false);
                    tvLogin.setText("服务商登录：未限制");
                    tvLogin.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 进件
     */
    public void requestChangeAgentIncomeLimit(String limit, String agent_line) {
        homePageModel.requestChangeAgentIncomeLimit(agent_id, limit, agent_line, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("设置成功");
                if ("1".equals(limit)) {
                    switchviewIncome.setOpened(true);
                    tvIncome.setText("服务商进件：已限制");
                    tvIncome.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                } else {
                    switchviewIncome.setOpened(false);
                    tvIncome.setText("服务商进件：未限制");
                    tvIncome.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 邀请
     */
    public void requestChangeAgentInviteLimit(String limit, String agent_line) {
        homePageModel.requestChangeAgentInviteLimit(agent_id, limit, agent_line, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("设置成功");
                if ("1".equals(limit)) {
                    switchviewInvite.setOpened(true);
                    tvInvite.setText("服务商邀请：已限制");
                    tvInvite.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                } else {
                    switchviewInvite.setOpened(false);
                    tvInvite.setText("服务商邀请：未限制");
                    tvInvite.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_setting, R.id.tv_setting2, R.id.tv_look_parents, R.id.tv_look_childs})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_setting:
                Intent intent = new Intent(mActivity, SettlementRulesActivity.class);
                intent.putExtra("agent_id", agent_id);
                startActivity(intent);
                break;
            case R.id.tv_setting2:
                intent = new Intent(mActivity, CodeCardAwardActivity.class);
                intent.putExtra("agent_id", agent_id);
                startActivity(intent);
                break;
            case R.id.tv_look_parents:
                intent = new Intent(mActivity, LookRankActivity.class);
                intent.putExtra("agent_id", agent_id);
                intent.putExtra("type", "parents");
                startActivity(intent);
                break;
            case R.id.tv_look_childs:
                intent = new Intent(mActivity, LookRankActivity.class);
                intent.putExtra("agent_id", agent_id);
                intent.putExtra("type", "childs");
                startActivity(intent);
                break;
        }
    }

    private void showPictureSelectorDialog(String type, String limit) {

        selectPicDialog = new BottomSheetDialog(mActivity, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        selectPicDialog.setCancelable(true);
        selectPicDialog.setCanceledOnTouchOutside(true);
        selectPicDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                cancelPictureDialog(type, limit);
            }
        });
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_bottom_select_limit, null);
        // 相册
        TextView album = view.findViewById(R.id.tv_select_pictrue_album);
        // 相机
        TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
        // 取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);

        album.setOnClickListener(v -> handlerPhotoAlbumClick(type, limit));
        camera.setOnClickListener(v -> handlerCameraClick(type, limit));
        cancel.setOnClickListener(v -> cancelPictureDialog(type, limit));

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }

    /**
     * 1-链式修改
     */
    private void handlerCameraClick(String type, String limit) {
        if ("Withdraw".equals(type)) {
            requestChangeAgentWithdrawLimit(limit, "1");
        } else if ("Login".equals(type)) {
            requestChangeAgentLoginLimit(limit, "1");
        } else if ("Income".equals(type)) {
            requestChangeAgentIncomeLimit(limit, "1");
        } else if ("Invite".equals(type)) {
            requestChangeAgentInviteLimit(limit, "1");
        }
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }

    /**
     * 0-单一修改
     */
    private void handlerPhotoAlbumClick(String type, String limit) {
        if ("Withdraw".equals(type)) {
            requestChangeAgentWithdrawLimit(limit, "0");
        } else if ("Login".equals(type)) {
            requestChangeAgentLoginLimit(limit, "0");
        } else if ("Income".equals(type)) {
            requestChangeAgentIncomeLimit(limit, "0");
        } else if ("Invite".equals(type)) {
            requestChangeAgentInviteLimit(limit, "0");
        }
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }

    private void cancelPictureDialog(String type, String limit) {
        if ("Withdraw".equals(type)) {
            if ("1".equals(limit)) {
                switchviewWithdraw.setOpened(false);
                tvWithdraw.setText("服务商提现：未限制");
                tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
            } else {
                switchviewWithdraw.setOpened(true);
                tvWithdraw.setText("服务商提现：已限制");
                tvWithdraw.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
            }
        } else if ("Login".equals(type)) {
            if ("1".equals(limit)) {
                switchviewLogin.setOpened(false);
                tvLogin.setText("服务商登录：未限制");
                tvLogin.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
            } else {
                switchviewLogin.setOpened(true);
                tvLogin.setText("服务商登录：已限制");
                tvLogin.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
            }
        } else if ("Income".equals(type)) {
            if ("1".equals(limit)) {
                switchviewIncome.setOpened(false);
                tvIncome.setText("服务商进件：未限制");
                tvIncome.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
            } else {
                switchviewIncome.setOpened(true);
                tvIncome.setText("服务商进件：已限制");
                tvIncome.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
            }
        } else if ("Invite".equals(type)) {
            if ("1".equals(limit)) {
                switchviewInvite.setOpened(false);
                tvInvite.setText("服务商邀请：未限制");
                tvInvite.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
            } else {
                switchviewInvite.setOpened(true);
                tvInvite.setText("服务商邀请：已限制");
                tvInvite.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
            }
        }
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }
}
